home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 April / CICA MS Windows - April 1993.iso / unzipped / programr / listings / ptv2n5 / dither1.c < prev    next >
Text File  |  1991-10-01  |  5KB  |  124 lines

  1. /*--------------------------------------------------------*/
  2. /* File: LISTING1.C   By: Marv Luse, Autumn Hill Software */
  3. /*                                                        */
  4. /* Desc: Code for computing 4x4 dither cells with either  */
  5. /*       16 linear steps or 8 logs steps, given a 4x4     */
  6. /*       dither matrix as input.                          */
  7. /*--------------------------------------------------------*/
  8. #include "stdlib.h"
  9. #include "stdio.h"
  10. #include "math.h"
  11.  
  12. /* types applicable to 4x4 dithering */
  13. typedef int DITHER_MATRIX[4][4];
  14. typedef unsigned char DITHER_CELL[4];
  15.  
  16. /* dit_mat determines dot-order within cell */
  17. DITHER_MATRIX dit_mat = {
  18.    {  9,  5, 16, 12 }, { 13,  1,  4,  8 },
  19.    {  6,  2,  3, 15 }, { 10, 14,  7, 11 },};
  20.  
  21. /* dit_cells contains 4x4 dither patterns */
  22. DITHER_CELL dit_cells[16] = {
  23.   { 0x00, 0x00, 0x00, 0x00 },{ 0x00, 0x00, 0x00, 0x00 }, /*  0,1  */
  24.   { 0x00, 0x00, 0x00, 0x00 },{ 0x00, 0x00, 0x00, 0x00 }, /*  2,3  */
  25.   { 0x00, 0x00, 0x00, 0x00 },{ 0x00, 0x00, 0x00, 0x00 }, /*  4,5  */
  26.   { 0x00, 0x00, 0x00, 0x00 },{ 0x00, 0x00, 0x00, 0x00 }, /*  6,7  */
  27.   { 0x00, 0x00, 0x00, 0x00 },{ 0x00, 0x00, 0x00, 0x00 }, /*  8,9  */
  28.   { 0x00, 0x00, 0x00, 0x00 },{ 0x00, 0x00, 0x00, 0x00 }, /* 10,11 */
  29.   { 0x00, 0x00, 0x00, 0x00 },{ 0x00, 0x00, 0x00, 0x00 }, /* 12,13 */
  30.   { 0x00, 0x00, 0x00, 0x00 },{ 0x00, 0x00, 0x00, 0x00 }, /* 14,15 */
  31. };
  32. /*--------------------------------------------------------*/
  33. /* determine dot count at each step of log intensity scl  */
  34. /*--------------------------------------------------------*/
  35. void compute_log_steps( int dot_cnt[], int nlvls, int ndots ) {
  36.     int    i;
  37.     double r, ri;
  38.  
  39. /* compute step intensity ratio */
  40.     r = pow( ndots, 1.0/(nlvls-1.0) );
  41.  
  42. /* index 0 always has 0 dots on */
  43.     dot_cnt[0] = 0;
  44.  
  45. /* compute number of dots in remaining levels */
  46.     for( i=1, ri=r;  i<nlvls;  i++, ri*=r )
  47.        dot_cnt[i] = (int) (ri + 0.5);
  48. }
  49. /*--------------------------------------------------------*/
  50. /* create an 8-level 4x4 logarithmic intensity dither     */
  51. /*--------------------------------------------------------*/
  52. void log_4x4_dither(  DITHER_MATRIX dm, DITHER_CELL dc[] ) {
  53.     int i, j, n, cnt[8];
  54.     unsigned char mask;
  55.  
  56. /* determine dots/cell for each level */
  57.     compute_log_steps( cnt, 8, 16 );
  58. /* determine dither cells */
  59.     for( n=0; n<8; n++ ) {            /* cell n           */
  60.        for( i=0; i<4; i++ ) {         /* byte i of cell n */
  61.           dc[n][i] = 0;
  62.           for( j=0; j<4; j++ ) {      /* bit j of byte i  */
  63.              mask = 0x08 >> j;
  64.              if( dm[i][j] <= cnt[n] ) dc[n][i] |= mask;
  65.           }
  66.        }
  67.     }
  68. }
  69. /*--------------------------------------------------------*/
  70. /* create a 16-level 4x4 linear intensity dither          */
  71. /*--------------------------------------------------------*/
  72. void lin_4x4_dither(  DITHER_MATRIX dm, DITHER_CELL dc[] ) {
  73.     int i, j, n;
  74.     unsigned char mask;
  75.  
  76. /* determine dither cells */
  77.     for( n=0; n<16; n++ ) {           /* cell n           */
  78.        for( i=0; i<4; i++ ) {         /* byte i of cell n */
  79.           dc[n][i] = 0;
  80.           for( j=0; j<4; j++ ) {      /* bit j of byte i  */
  81.              mask = 0x08 >> j;
  82.              if( dm[i][j] <= n ) dc[n][i] |= mask;
  83.           }
  84.        }
  85.     }
  86. }
  87. /*--------------------------------------------------------*/
  88. /* print the data-initialization of a 4x4 dither set      */
  89. /*--------------------------------------------------------*/
  90. void print_dither(  char *name, DITHER_CELL dc[], int ncells ) {
  91.     int i, j;
  92.  
  93.     printf( "DITHER_CELL %s[] =\n", name );
  94.     printf( "{\n" );
  95.     for( i=0; i<ncells; i++ ) {
  96.        printf( "   {" );
  97.        for( j=0; j<4; j++ ) printf( " 0x%02X,", dc[i][j] );
  98.        printf( " },\n" );
  99.     }
  100.     printf( "};\n" );
  101. }
  102. /*--------------------------------------------------------*/
  103. /* main to exercise the code...                           */
  104. /*--------------------------------------------------------*/
  105. void main( void ) {
  106.      int i, j;
  107.  
  108.      printf( "4x4 Dither Matrix...\n" );
  109.      printf( "\n" );
  110.      for( i=0; i<4; i++ ) {
  111.         printf( "  %c", 179 );
  112.         for( j=0; j<4; j++ ) printf( " %2d", dit_mat[i][j] );
  113.         printf( " %c\n", 179 );
  114.      }
  115.      printf( "\n" );
  116.      printf( "4x4 16-level Linear Dither...\n" ); printf( "\n" );
  117.      lin_4x4_dither( dit_mat, dit_cells );
  118.      print_dither( "lin_4x4", dit_cells, 16 ); printf( "\n" );
  119.      printf( "4x4 8-level Logarithmic Dither...\n" );
  120.      printf( "\n" );
  121.      log_4x4_dither( dit_mat, dit_cells );
  122.      print_dither( "log_4x4", dit_cells, 8 ); printf( "\n" );
  123. }
  124.